package com.wondertek.liveroom;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.jasontools.data.LiveChannelPc;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

public class MyLiveIndexTopViewPagerAdapter extends PagerAdapter {

	private Context context = null;
	private List<LiveChannelPc> listData = null;
	private JSONArray jsonData = null;
	
	public MyLiveIndexTopViewPagerAdapter(Context context, List<LiveChannelPc> list) {
		this.context = context;
		this.listData = list;
	}
	
	public void setData(List<LiveChannelPc> list) {
		this.listData = list;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		PageItemViewLiveIndexTop view = (PageItemViewLiveIndexTop) object;
		view.recycle();
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		final int pos = position;
		PageItemViewLiveIndexTop view = new PageItemViewLiveIndexTop(context);
		view.setItemData(this.listData.get(position));
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startLiveActivity(pos);
			}			
		});
		((ViewPager)container).addView(view);
		return view;
	}

	@Override
	public int getCount() {
		return this.listData.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}
	
	private void startLiveActivity(int position) {
		LiveChannelPc live = listData.get(position);
		Intent intentLive = new Intent(context, LiveActivity.class);
		Bundle bundle = new Bundle();
		bundle.putString("Title", live.getTitle());
		bundle.putInt("Id", live.getId());
		bundle.putString("Jid", live.getJid());
		bundle.putString("Description", live.getDescription());
		bundle.putString("PlayerUrl", live.getRtmpUrl() + "/" + live.getRtmpCode());
		bundle.putInt("Person", live.getPerson());
		intentLive.putExtras(bundle);
		context.startActivity(intentLive);
	}
}
